package kin.bean.security;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;
import javax.persistence.Id;

import kin.annotation.system.EmbeddedIdColumn;
import kin.annotation.system.KinColumn;
import kin.annotation.system.KinForm;
import kin.annotation.system.Trigger;





@Entity
@Table(name="t_acl")
public class Acl implements Serializable{
	//主体类型是角色
	public static final String TYPE_ROLE = "Role";
	//主体类型是用户
	public static final String TYPE_USER = "User";
	
	/*
	 * *
	 * @hibernate.id generator-class="native"
	 */
	//@EmbeddedId
	@Id
	@KinColumn(hidden=true,editor=false,xtype="numberfield",sort=2,isEmbeddedId=true,embeddedIdColumns={
			@EmbeddedIdColumn(fieldName = "", header = "")
			}
	)

	//@SerializableClassAttribute(hidden=true,editor=false)
	private AclId aclId;
	
	public AclId getAclId() {
		return aclId;
	}
	public void setAclId(AclId aclId) {
		this.aclId = aclId;
	}

	/**
	 * 
	 * 授权状态,用后五位表示CRUDP操作
	 */
	@Column(nullable=false,name="aclState")
	@KinColumn(header="label.role.add",editor=true,sort=1,xtype="checkbox")
	private int aclState;
	
	/*
	 * *是否继承,0表示不继承,1表示继承
	 * @hibernate.property
	 */
	@Column(name="extendsState")
	private int extendsState;
	
	/**
	 * ACL是
	 */
	public static final int ACL_TRI_STATE_EXTENDS =1;
	
	/**
	 * ACL��״̬Ϊ���̳У�����Ч���жϵ�ʱ��ֱ�Ӹ��aclState�ж���Ȩ��
	 */
	public static final int ACL_TRI_STATE_UNEXTENDS = 0;
	
	/**
	 * 允许授权
	 */
	public static final int ACL_YES = 1;
	
	/**
	 * 不允许授权
	 */
	public static final int ACL_NO = 0;
	
	/**
	 * 授权不确定
	 */
	public static final int ACL_NEUTRAL = -1;
	


	
	public int getAclState() {
		return aclState;
	}
	public void setAclState(int aclState) {
		this.aclState = aclState;
	}
	
	
	public int getExtendsState() {
		return extendsState;
	}
	public void setExtendsState(int extendsState) {
		this.extendsState = extendsState;
	}
	
	public void setPermission(int permission,boolean yes){
		int temp = 1;
		temp = temp << permission;
		if(yes){
			aclState |= temp;
		}else{
			aclState &= ~temp;
		}
	}
	
	public int getPermission(int permission){
		/*
		 * 如果是继承,则返回为授权信息
		 * */
		if(extendsState == ACL_TRI_STATE_EXTENDS){
			return ACL_NEUTRAL;
		}
		
		int temp = 1;
		temp = temp << permission;
		temp &= aclState;
		if(temp != 0){
			return ACL_YES;
		}
		return ACL_NO;
	}
	
	/**
	 * acl* 
	 * 
	 * @param yes true��ʶ�̳У�false��ʾ���̳�
	 */
	public void setExtends(boolean yes){
		if(yes){
			extendsState = ACL_TRI_STATE_EXTENDS;
		}else{
			extendsState = ACL_TRI_STATE_UNEXTENDS;
		}
	}
	
	public static int getPermissionByModule(String module_url){
	int i=-1;

			String sa =module_url.split("/")[2];
		switch(sa){
		case "add":i=Permission.Create;break;
		case "delete":i=Permission.Delete;break;
		case "update":i=Permission.Update;break;
		case "list":i=Permission.Read;break;
		case "upload":i=Permission.Upload;break;
		case "cropImage":i=Permission.cropImage;break;
		case "serach":i=Permission.serach;break;
			}
		return i;
		
	}
	
}
